package com.wskj.web.controller.system;

import com.wskj.common.annotation.Log;
import com.wskj.common.annotation.RepeatSubmit;
import com.wskj.common.core.controller.BaseController;
import com.wskj.common.core.domain.AjaxResult;
import com.wskj.common.core.page.TableDataInfo;
import com.wskj.common.enums.BusinessType;
import com.wskj.common.exception.CustomException;
import com.wskj.common.utils.poi.ExcelUtil;
import com.wskj.system.bo.SysAboutUsAddBo;
import com.wskj.system.bo.SysAboutUsEditBo;
import com.wskj.system.bo.SysAboutUsQueryBo;
import com.wskj.system.service.ISysAboutUsService;
import com.wskj.system.vo.web.SysAboutUsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 关于我们Controller
 *
 * @author wsdz
 * @date 2022-12-13
 */
@Api(value = "关于我们控制器", tags = {"关于我们管理"})
@AllArgsConstructor
@RestController
@RequestMapping("/system/about")
public class SysAboutUsController extends BaseController {

    private final ISysAboutUsService iSysAboutUsService;

    /**
     * 查询关于我们列表
     */
    @ApiOperation("查询关于我们列表")
    @PreAuthorize("@ss.hasPermi('system:about:list')")
    @GetMapping("/list")
    public TableDataInfo<SysAboutUsVo> list(@Validated SysAboutUsQueryBo bo) {
        return iSysAboutUsService.queryPageList(bo);
    }

    /**
     * 导出关于我们列表
     */
    @ApiOperation("导出关于我们列表")
    @PreAuthorize("@ss.hasPermi('system:about:export')")
    @Log(title = "关于我们", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<Void> export(@Validated SysAboutUsQueryBo bo) {
        List<SysAboutUsVo> list = iSysAboutUsService.queryList(bo);
        ExcelUtil<SysAboutUsVo> util = new ExcelUtil<>(SysAboutUsVo.class);
        return util.exportExcel(list, "关于我们");
    }

    /**
     * 获取关于我们详细信息
     */
    @ApiOperation("获取关于我们详细信息")
    @PreAuthorize("@ss.hasPermi('system:about:query')")
    @GetMapping("/{usId}")
    public AjaxResult<SysAboutUsVo> getInfo(@NotNull(message = "主键不能为空")
                                                  @PathVariable("usId") Long usId) {
        return AjaxResult.success(iSysAboutUsService.queryById(usId));
    }

    /**
     * 新增关于我们
     */
    @ApiOperation("新增关于我们")
    @PreAuthorize("@ss.hasPermi('system:about:add')")
    @Log(title = "关于我们", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated @RequestBody SysAboutUsAddBo bo) {
        return toAjax(iSysAboutUsService.insertByAddBo(bo) ? 1 : 0);
    }

    /**
     * 修改关于我们
     */
    @ApiOperation("修改关于我们")
    @PreAuthorize("@ss.hasPermi('system:about:edit')")
    @Log(title = "关于我们", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated @RequestBody SysAboutUsEditBo bo) {
        return toAjax(iSysAboutUsService.updateByEditBo(bo) ? 1 : 0);
    }

    /**
     * 删除关于我们
     */
    @ApiOperation("删除关于我们")
    @PreAuthorize("@ss.hasPermi('system:about:remove')")
    @Log(title = "关于我们" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{usIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                       @PathVariable Long[] usIds) {
        return toAjax(iSysAboutUsService.deleteWithValidByIds(Arrays.asList(usIds), true) ? 1 : 0);
    }

    /**
     * 导入关于我们数据
     */
    @ApiOperation("导入关于我们数据")
    @ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "file")
    @PreAuthorize("@ss.hasPermi('system:about:import')")
    @Log(title = "关于我们" , businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult<Void> importData(MultipartFile file) {

        ExcelUtil<SysAboutUsVo> util = new ExcelUtil<>(SysAboutUsVo.class);
        List<SysAboutUsVo> sysAboutUsList;
        try {
            sysAboutUsList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            e.printStackTrace();
            throw new CustomException("文件解析异常！message：" + e.getMessage());
        }
        String message = iSysAboutUsService.importSysAboutUs(sysAboutUsList);
        return AjaxResult.success(message);

    }

    /**
     * 下载模板
     *
     * @return AjaxResult 模板文件
     */
    @ApiOperation("下载模板")
    @GetMapping("/importTemplate")
    public AjaxResult importTemplate() {
        ExcelUtil<SysAboutUsVo> util = new ExcelUtil<>(SysAboutUsVo.class);
        return util.importTemplateExcel("关于我们数据");
    }
}
